package vfs.client.command;

import vfs.client.rmi.ClientRmiManager;
import vfs.common.command.AbstractCommand;
import vfs.common.command.Command;
import vfs.common.tty.Tty;

import java.rmi.RemoteException;

/**
 * User: serge
 * Date: 19.11.13
 * Time: 5:10
 */
public class QuitCommand extends AbstractCommand implements Command
{
    @Override
    public void execute()
    {
        try
        {
            boolean needCloseSession = ClientRmiManager.getInstance().isSessionStarted();
            boolean sessionClosed = false;

            if (needCloseSession)
            {
                sessionClosed = ClientRmiManager.getInstance().closeSession();
            }

            if (sessionClosed)
            {
                Tty.setUsername(null);
            }

            if (!needCloseSession)
            {
                Tty.stop();
                Tty.println("Exiting...");
                System.exit(0);
            }
        }
        catch (RemoteException e)
        {
            e.printStackTrace();  //Todo generated catch
        }
    }
}
